<template>

	<view>
		<z-paging-swiper>
			<view style="background-color: white;" slot="top">
				<view class="header">
					<!-- <image src="../../static/left.png" class="back"></image> -->
					<image src="../../static/home_title.png" class="header_title"></image>
				</view>
				<view class="search">
					<image src="../../static/serch.png" class="search_img"></image>
					<input class="input" placeholder="请输入查询内容" @click="doSearch" :disabled="true" />
				</view>

				<z-tabs ref="tabs" :list="tabList" :current="isChange" @change="tabsChange" barWidth="70" barHeight="5"
					bottomSpace="0" />
			</view>

			<view></view>
			<swiper class="swiper" :current="isChange" @change="swiperChange" @transition="swiperTransition"
				@animationfinish="swiperAnimationfinish">
				<swiper-item v-for="(item, index) in tabList" :key="index">
					<swiper-home-waterSavingNews v-if="index == 0 || index == 1" :tabIndex="index"
						:currentIndex="isChange" :status="status">
					</swiper-home-waterSavingNews>
					<swiper-home-PoliciesRegulations v-if="index == 2" :tabIndex="index" :currentIndex="isChange">
					</swiper-home-PoliciesRegulations>
					<swiper-home-Notice v-if="index == 3" :tabIndex="index" :currentIndex="isChange">
					</swiper-home-Notice>
				</swiper-item>
			</swiper>
		</z-paging-swiper>
		<suspended :jsdm="jsdm"></suspended>
	</view>

</template>


<script>
	import SM from '../../js/sm2/build/SM.js'
	import {
		tools
	} from '../../common/sm2.js'

	import {
		appUserLogin
	} from '../../common/api.js'
	import {
		aes_encrypt,
		aes_Decrypt
	} from '../../common/aesutils.js'

	const appword = "aKd20dbGdFvmuwrt" // 固定值

	export default {
		data() {
			return {
				tabList: ['节水要闻', '节水科普', '政策法规', '通知公告'],
				isChange: 0,
				status: 888,
				jsdm: '节水代码',
			}
		},

		onLoad() {
			this.getUserInfo()
		},

		onShow() {
			if (uni.getStorageSync('username')) {
				this.jsdm = uni.getStorageSync('username')
				console.log("首页-----", this.jsdm)
			}
		},


		methods: {

			//tabs通知swiper切换
			tabsChange(index) {
				this.isChange = index;
			},
			//swiper change时触发
			swiperChange(e) {
				// #ifndef APP-PLUS || H5 || MP-WEIXIN || MP-QQ
				let current = e.target.current || e.detail.current;
				this.tabsChange(current);
				// #endif
			},
			//swiper滑动中
			swiperTransition(e) {
				this.$refs.tabs.setDx(e.detail.dx);
			},
			//swiper滑动结束
			swiperAnimationfinish(e) {
				this.isChange = e.detail.current;
				this.$refs.tabs.unlockDx();
			},

			doSearch() {
				uni.navigateTo({
					url: '/pages/Home/Search/Search?tab=' + this.isChange
				})
			},

			getUserInfo() {
				var that = this
				lightAppJssdk.user.getUserInfoWithEncryptedParamByAppId({
					appId: "hqyhxxjmdemogggid", // 接入方在成功创建应用后自动生成 （此处已对接，使用demo即可）
					success: function(data) {
						if (data == '未登录') that.onLoginApp()
						else {
							console.log('加密原始参数', data)
							if (typeof data == 'string')
								data = JSON.parse(data)
							console.log('加密原始参数===string', data)
							// 对于该方法的返回结果,需要进行两步解密才能得到明文用户信息,
							// 先对返回结果进行sm2解密,需要用到 接入方在创建应用时,选择应用的加密方式为sm2,并成功创建应用后,拿到的应用私钥来进行解密 （此处已对接，使用demo 即可）
							const sm2_privateKey =
								'165671ec5e9fbfa45cbd719876208718641792d936fe68447560117529f80ca5'
							let sm2_encrypt_result = data.data
							let sm2_decrypt_result = tools.isSm2Dm(sm2_encrypt_result, sm2_privateKey)

							sm2_decrypt_result = JSON.parse(sm2_decrypt_result)

							// 其次,对sm2解密后的结果进行 aes解密 
							// aes解密需要用到 appword , 为固定值,使用示例代码中的即可
							let aes_encrypt_result = sm2_decrypt_result.data
							let aes_decrypt_result = aes_Decrypt(aes_encrypt_result, appword)

							console.log('手机号======', JSON.parse(aes_decrypt_result).mobile)

							that.login(JSON.parse(aes_decrypt_result).mobile)

							uni.setStorageSync('phone', JSON.parse(aes_decrypt_result).mobile)
							// findCorporationByToken(); // 法人企业信息
						}
					},
					fail: function(data) {
						console.log('fail', data)
					}
				});
			},


			async login(phone) {
				uni.removeStorageSync('token')
				uni.removeStorageSync('username')
				//uni.removeStorageSync('jsdmList')
				var data = {
					Number: aes_encrypt(phone)
					//Number: aes_encrypt('15853135062')
				}
				let res = await appUserLogin(data)

				console.log('登录======', res)

				if (res.status === 200) {
					uni.setStorageSync('token', res.response.token)
					uni.setStorageSync('username', res.response.username)           
					uni.setStorageSync('jsdmList', JSON.stringify(res.response.jsdmList))
					this.jsdm = res.response.username
				} else if (res.status === 500) {
					console.log('登录======', res.msg)
					uni.showToast({
						title: res.msg,
						icon: 'error',
						duration: 6000
					})
				}
				this.status = res.status
			},


			/**
			 * 法人企业信息
			 */
			// function findCorporationByToken() {
			//     let signurl = 'https://' + urldomaincreatesign;
			//     let gatewayurl = 'https://' + urldomaingateway;
			//     let appid = 'hqyhxxjmdemogggid';
			//     let interfaceid = 'findCorporationByToken';
			//     let timestamp = Date.parse(new Date());
			//     let appmark = "sdzwapp"
			//     let appword = "aKd20dbGdFvmuwrt"
			//     let params = JSON.stringify({
			//         token: userToken,
			//         pageindex: '1',
			//         pagenum: '5',
			//     });
			//     let interfacecontent = JSON.stringify({
			//         appmark: appmark,
			//         time: timestamp,
			//         sign: hex_md5(appmark + appword + timestamp),
			//         servicename: "findCorporationByToken",
			//         params: params
			//     });
			//     let fromport = '1';
			//     vaildInterfacefn(appid, interfaceid, interfacecontent, fromport, signurl, gatewayurl).then(value => {
			//         console.log(value, 'value')
			//         var moduleResult = value
			//         if (moduleResult == false) {//失败返回
			//             return;
			//         } else {//接口成功返回
			//             const sm2_privateKey = '165671ec5e9fbfa45cbd719876208718641792d936fe68447560117529f80ca5'
			//             let sm2_encrypt_result = value;
			//             let sm2_decrypt_result = sm2_Decrypt(sm2_encrypt_result, sm2_privateKey);
			//            console.log(sm2_decrypt_result, 'sm2_decrypt_result')
			//         }
			//     })
			// }

			/**
			 * 使用jssdk调用登录页面
			 */
			onLoginApp() {
				var that = this
				lightAppJssdk.user.loginapp({
					success: function(data) {
						if (data == "未登录") {
							//取消登录或登录失败，关闭页面
							that.oncloseWindow()
						} else {
							that.getUserInfo()
						}
					},
					fail: function(data) {
						//关闭页面
						that.oncloseWindow()
					}
				})
			},

			/**
			 * 关闭容器
			 */
			oncloseWindow() {
				lightAppJssdk.navigation.close({
					success: function(data) {},
					fail: function(data) {}
				})
			}
		}
	}
</script>

<style>
	page {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
	}

	.header {
		width: 100%;
		height: 88rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		padding-top: 0rpx;
	}

	.back {
		width: 23px;
		height: 23px;
	}

	.header_title {
		width: 133px;
		height: 35px;
	}

	.search {
		width: 702rpx;
		height: 66rpx;
		background: #FFFFFF;
		border-radius: 33rpx;
		border: 1rpx solid #1677FF;
		margin-left: 24rpx;
		margin-right: 24rpx;
		margin-top: 24rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		margin-bottom: 20rpx;
	}

	.input {
		width: 100%;
		height: 42rpx;
		font-size: 30rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #999999;
		line-height: 42rpx;
		margin-left: 17rpx;
	}

	.search_img {
		width: 29rpx;
		height: 29rpx;
		margin-left: 23rpx;
	}

	scroll-view {
		white-space: nowrap;
		/* position: fixed; */
	}

	.swiper {
		flex: 1;
		/* #ifndef APP-NVUE */
		height: 100%;
		/* #endif */

	}
</style>